package cn.itcast.controller;

import cn.itcast.pojo.Product;
import cn.itcast.service.ProductService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping
    public Page<Product> findByPage(
          @RequestParam (defaultValue = "1") Integer page,
          @RequestParam (defaultValue = "10") Integer pageSize)
    {
        Page<Product> products = productService.findByPage(page, pageSize);
        return products;
    }
    @GetMapping("/{id}")
    public Product findById(@PathVariable Integer id)
    {
        Product product = productService.findById(id);
        return product;
    }
    @PostMapping
    public String addProduct(@RequestBody Product product)
    {
        boolean add = productService.addProduct(product);
        return add ? "添加商品成功" : "添加商品失败";
    }
    @PutMapping
    public String updateProduct(@RequestBody Product product)
    {
        boolean result = productService.updateProduct(product);
        return result ? "修改商品成功" : "修改商品失败";
    }
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Integer id)
    {
        boolean result = productService.deleteById(id);
        return result ? "删除商品成功" : "删除商品失败";
    }
}
